package com.issuser.appdeducations_2.mina;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.issuser.appdeducations_2.App;
import com.issuser.appdeducations_2.activity.MsgBean;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by issuser on 2018/7/18.
 */


public class FirstClientHandler extends IoHandlerAdapter {
    private App app;
    private final Gson gson;
    private long startTime = 0;
    boolean isRunfinish = true;//防止第一次计时后,还没有完成,第二次通信又开始计时
    private final Timer timer;
    public static final String deviceID = "79486049";//红米
//    public static final String deviceID = "57c0604c";//小米

    public FirstClientHandler(App app) {
        gson = new Gson();
        timer = new Timer();
        this.app = app;
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        System.out.println("here got a exception!!!");
        super.exceptionCaught(session, cause);
    }

    @Override
    public void messageSent(IoSession session, Object message) throws Exception {
        Log.w("yy", "messageSent");
        super.messageSent(session, message);
    }

    @Override
    public void sessionCreated(final IoSession session) throws Exception {
        Log.w("yy", "sessionCreated");
        super.sessionCreated(session);
        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                session.write("tiaotiaotiaotiao");
            }
        }, 1000, 3000);
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        Log.w("yy", "sessionClosed");
        super.sessionClosed(session);
    }

    /**
     * {
     * devicesID:""设备ID
     * type:
     * * 0:PC_退出
     * * 1.PC_执行下一个
     * * 2.PC_关闭指定app
     * * 3.APP_开始计时
     * * 4.APP_结束计时,查询短信
     * Pc6Entity:
     * * id:"",在-1的情况下,表示测试结束
     * }
     */
    @Override
    public void messageReceived(final IoSession session, Object message) throws Exception {
        super.messageReceived(session, message);
        isRunfinish = false;
        String content_Socket = URLDecoder.decode(message.toString(), "utf-8");
        if (content_Socket == null || content_Socket.equals(""))
            return;
        Log.w("yy", "Socket内容:" + content_Socket);
        MsgBean msgbean = gson.fromJson(content_Socket, MsgBean.class);
        if (msgbean != null) {
            switch (msgbean.getType()) {
                case 3:
                    System.out.print("开始5分钟计时");
                    startTime = System.currentTimeMillis();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            try {
                                isRunfinish = true;
                                MsgBean slelectSMS = new MsgBean();
                                slelectSMS.setDevicesID(deviceID);
                                slelectSMS.setType(2);
                                String SMScontent = "<<<<<短信内容";
//                                    String SMScontent = "<<<<<短信内容" + app.slelectSMS(startTime, System.currentTimeMillis());
                                if (!TextUtils.isEmpty(SMScontent)) {
                                    slelectSMS.setSMScontent(SMScontent);
                                }
                                session.write(URLEncoder.encode(gson.toJson(slelectSMS), "utf-8"));
                                Thread.sleep(1000);
                                slelectSMS.setType(1);
                                slelectSMS.setSMScontent("");
                                session.write(URLEncoder.encode(gson.toJson(slelectSMS), "utf-8"));
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                                Log.w("yy", e.getMessage());
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                Log.w("yy", e.getMessage());
                            }
                        }
                    }, 1000 * 20);
                    break;
            }
        }
    }

}